#!/bin/bash

file_name=functions.sh
funcs=`which $file_name`
if [ "$funcs" != "" ] ;then
  echo "funcs=$funcs"
  . $funcs
else
  funcs=`dirname $0`/$file_name
  echo "$PWD"
  echo "funcs=$funcs"
  if [ "$funcs" != "" ] ;then
    . $funcs
  else
    exit
  fi
fi

#变量定义
source_top_dir=
ota_tool=
compiler_out_dir=
host_linux_dir=
signature=
full_update_ile=
release_dir=
target_mount_backup=
target_bak_dir=

#函数部分
function showHelp()
{
	echoTextRed "`basename $0` --work-copy /home/compiler/workspace/gphone/MT6575/PD1203/ALPS.ICS.MP.V2_W_20120504_automake -d bbk75_cu_ics -p PD1203" 
	exit 1
}

function gen_update_patchzip()
{
	local zipfrom="$1"
	local zipto="$2"
	local target="$3"
	mkdir -p `dirname $target`
#	echo "$ota_tool -i $zipfrom -p $host_linux_dir -k $signature $zipto $target"
	$ota_tool -i $zipfrom -p $host_linux_dir -k $signature $zipto $target
}


#脚本入口
while [ $# != 0 ]
do
	case $1 in
	--work-copy | --work_copy)
		shift
		work_copy=$1
		;;
   	-d)
	    shift
	    product_name=$1
	    ;;
   	-p)
	    shift
	    project_name=$1
	    ;;
	--help | --h)
		shift
		showHelp
		;;
	*)
		shift
		work_copy=$1
		;;
	esac
	shift
done

if [ "$work_copy" = "" ];then
	work_copy=`pwd`
	work_copy=`question "请手工指定工作副本的全路径" 2 $work_copy`
	echo $work_copy
	if ! is_svn_working_copy $work_copy;then
		echoTextRed "不是工作拷贝，退出"
		exit 1
	fi
fi

if [ "$product_name" = "" ];then
	echoTextRed "未指定产品名称，读取路径下是否存在产品文件以确定产品项目"
	if ! test -f $work_copy/build/target/product/bbk*.mk;then
		echoTextRed "不存在产品文件，退出，请手工指定，查看帮助 `basename $0` --help"
	else
		product_file=`ls $work_copy/build/target/product/bbk*.mk | xargs basename`
		echoTextPurple "找到产品文件$work_copy/build/target/product/$product_file" 
		product_name=`basename $product_file .mk`
		echo "product_file=$product_file,product_name=$product_name"
	fi
fi
if [ "$project_name" = "" ];then
	echoTextRed "未指定项目名称，退出，请查看帮助 `basename $0` --help"
	exit 1
fi

if ! test -f "$work_copy/out/target/product/$product_name/system/build.prop";then
	echoTextRed "no build.prop 请确认本地副本编译完成"
	exit 1
fi

source_top_dir="$work_copy"
compiler_out_dir="${work_copy}/out/target/product/${product_name}/"
ota_tool="${work_copy}/build/tools/releasetools/ota_from_target_files"
host_linux_dir="${work_copy}/out/host/linux-x86"
signature="${work_copy}/build/target/product/security/testkey"
full_update_ile="${work_copy}/out/target/product/${product_name}/${product_name}-ota-*.zip"


hw_version=`cat $work_copy/out/target/product/$product_name/system/build.prop | grep ro.hardware.bbk | awk -F '=' '{print $2}'`
sw_version=`cat $work_copy/out/target/product/$product_name/system/build.prop | grep ro.build.version.bbk | awk -F '=' '{print $2}'`
product_model=`cat $work_copy/out/target/product/$product_name/system/build.prop | grep ro.product.model.bbk | awk -F '=' '{print $2}'`
echoTextPurple "hw_version=$hw_version,sw_version=$sw_version,product_model=$product_model"
release_folder_name=`echo "$sw_version" | awk -F "_" '{print $3}'`
echo "release_folder_name=$release_folder_name"
release_dir="$HOME/workspace/gphone/release/${project_name}/upgrade/"
target_mount_backup="/mnt/smartbackup/smb/upgrade/"
target_bak_dir="${target_mount_backup}/${project_name}/upgrade/"

if ! test -d "${target_mount_backup}";then
	echoTextRed "${target_mount_backup}目录不存在，无法进行target包的备份，请挂载smartbackup"
	exit
else
	if ! test -d "${target_bak_dir}/release_folder_name/";then
		echoTextRed "${target_bak_dir}/${release_folder_name}/ 目录不存在，创建release目录"
		mkdir -pv ${target_bak_dir}/${release_folder_name}/
	fi
fi

if ! test -d "${release_dir}/${release_folder_name}";then
	echoTextRed "${release_dir}/$release_folder_name/目录不存在，创建release目录"
	mkdir -pv ${release_dir}/${release_folder_name}
fi

url_of_work_dir=`sh_get_svn_url  $work_copy`
echo "url_of_work_dir=$url_of_work_dir"

framework_curr_version=`sh_get_svn_committed_no_from_url $url_of_work_dir`
echo "framework_curr_version=$framework_curr_version"
framework_head_version=`sh_get_svn_head_no_of_copy $work_copy`
echo "framework_head_version=$framework_head_version"

result=`question "是否拷贝制作压缩包相关文件，target.zip和ota.zip?" 1 no`
if [ "$result" == "yes" ]; then 
	framework_version="`inputNumber "请确定本版软件框架仓的SVN版本号:" ${framework_head_version} ${framework_curr_version} ${framework_curr_version}`"
	echoTextRed "STARTING TO COPY FILES "
	cp -rvfp ${compiler_out_dir}/obj/PACKAGING/target_files_intermediates/${product_name}-target_files-*.zip ${release_dir}/${release_folder_name}/target-files_${sw_version}_r${framework_version}.zip
	cp -rvfp ${compiler_out_dir}/obj/PACKAGING/target_files_intermediates/${product_name}-target_files-*.zip ${target_bak_dir}/${release_folder_name}/target-files_${sw_version}_r${framework_version}.zip
	cp -rvfp ${compiler_out_dir}/bbk*ota*.zip ${release_dir}/${release_folder_name}/${sw_version}-update-full_r${framework_version}.zip 

fi

echo "release_dir=$release_dir"
all_preversion=`ls -F $release_dir/ | grep '/' |  tr "\n" " "| tr -d '/'`
num_all_preversion=`echo $all_preversion | wc -w`
#echo "num_all_preversion=$num_all_preversion"
if [ $num_all_preversion -le 1 ];then
	echoTextRed "无较早的软件版本，无须制作增量升级包...脚本退出...."
	exit 1;
fi

default_ver=${#all_preversion[*]}
sel_ver="`getChoice "请指定待制作升级包的版本:" all_preversion $default_ver`"
echo "you selected $sel_ver"
all_prever=(`echo ${all_preversion[@]} | sed "s/${sel_ver}/ /g"` )
echo "${allPreVer[@]}=${all_prever[@]}"

all_sel_prever=( `question "请设定较旧的版本(多个版本间使用空格分开) " 2 "${all_prever[*]}"`)
echo "you selected all_sel_prever=$all_sel_prever"


for v in ${all_sel_prever[@]}; do
	echoTextBlue "------------版本$sel_ver 对 版本$v 的升级包------------\n-_-"
#	echo "$release_dir/$v/target-files_*.zip"
	if [ ! -f $release_dir/$v/target-files_*.zip ]; then
		echoTextRed "版本 $v 无升级所需的压缩包文件!!!\n-_-||"
	else
		svnversource="`ls $release_dir/$v/target-files_*.zip | xargs basename | awk -F '_' '{print $5}' | tr -d '.zip'`"
		svnvertarget="`ls $release_dir/${sel_ver}/target-files_*.zip | xargs basename | awk -F '_' '{print $5}' | tr -d '.zip'`"
#		echo "svnversource=$svnversource,svnvertarget=$svnvertarget"
		echoTextCyan "gen_update_patchzip   $release_dir/$v/target-files_*.zip ${release_dir}/$sel_ver/target-files_*.zip ${release_dir}/$sel_ver/${product_model}-${hw_version}-update-patch_${sel_ver}_for_${v}_${svnvertarget}_for_${svnversource}.zip"
		gen_update_patchzip   $release_dir/$v/target-files_*.zip ${release_dir}/$sel_ver/target-files_*.zip ${release_dir}/$sel_ver/${product_model}-${hw_version}-update-patch_${sel_ver}_for_${v}_${svnvertarget}_for_${svnversource}.zip
	fi
done
echoTextRed "!!! ALL FINISHED !!!"

